package org.dydl.service.system.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dydl.common.dto.ResponseData;
import org.dydl.common.dto.ResponseMessage;
import org.dydl.dao.system.repository.IDeliveryDetailRepository;
import org.dydl.dao.system.repository.IGoodsRepository;
import org.dydl.dao.system.repository.IOrderDetailRepository;
import org.dydl.dao.system.repository.IOrderRepository;
import org.dydl.dao.system.repository.IProduceRepository;
import org.dydl.dao.system.repository.IProductionDetailRepository;
import org.dydl.dao.system.repository.ITranslateRepository;
import org.dydl.model.system.Delivery;
import org.dydl.model.system.DeliveryDetail;
import org.dydl.model.system.vo.DeliveryVO;
import org.dydl.service.system.ITranslateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TranslateServiceImpl implements ITranslateService {
	
	@Autowired
	private ITranslateRepository iTranslateRepository;
	
	@Autowired
	private IDeliveryDetailRepository iDeliveryDetailRepository;
	
	@Autowired
	private IOrderRepository iOrderRepository;
	
	@Autowired
	private IGoodsRepository iGoodsRepository;
	
	@Autowired
	private IProduceRepository iProduceRepository;
	
	@Autowired
	private IProductionDetailRepository iProductionDetailRepository;
	
	@Autowired
	private IOrderDetailRepository iOrderDetailRepository;
	
	private ResponseMessage res = new ResponseMessage();

	@Override
	public ResponseMessage list(int page ,int rows ,String deliveryNoteNo) {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Delivery> pageList = null;
		if (StringUtils.isNotBlank(deliveryNoteNo)) {
			pageList = iTranslateRepository.getDeliveryListLike(String.format("%s%s%s", "%", StringUtils.trim(deliveryNoteNo), "%"),
					pageable);
		} else {
			pageList = iTranslateRepository.getDeliveryList(pageable);
		}
		return new ResponseMessage(true, new ResponseData<Delivery>(page, pageList.getTotalPages(),
				((int) pageList.getTotalElements()), pageList.getContent()));
	}

	@Override
	@Transactional
	public ResponseMessage save(DeliveryVO deliveryVO) {
		
		try {
			iDeliveryDetailRepository.deleteDetail(deliveryVO.getDeliveryNoteNo());
			
			iTranslateRepository.save(deliveryVO.getDelivery());
			
			iDeliveryDetailRepository.save(deliveryVO.getArr());

			res.setSuccess(true);
		} catch (Exception e) {
			res.setMsg(e.getMessage());
			res.setSuccess(false);
		}
		return res;
	}

	@Override
	public void delete(int id) {
		iTranslateRepository.delete(id);
	}

	@Override
	public List<DeliveryDetail> getDetailList(String deliveryNoteNo) {
		
		List<DeliveryDetail> res = iDeliveryDetailRepository.getDetailList(deliveryNoteNo);
		
		return res;
	}

	@Override
	public Delivery findByDeliveryNoteNo(String deliveryNoteNo) {
		Delivery delivery = iTranslateRepository.findByDeliveryNoteNo(deliveryNoteNo);
		
		return delivery;
	}

	@Override
	@Transactional
	public ResponseMessage changeExamine(Integer id,String orderNo,String deliveryNoteNo) {
		try {
			Integer inStockQty ;
			Integer already;
			int item = 0;
			List<DeliveryDetail> detailList = iDeliveryDetailRepository.getDetailList(deliveryNoteNo);
			for (DeliveryDetail deliveryDetail : detailList) {
				inStockQty = iGoodsRepository.findInStockQTYByUUID(deliveryDetail.getGoodsUuid());
				already = iProductionDetailRepository.getAlready(deliveryDetail.getOrderDetailUuid());
				
				if(inStockQty - deliveryDetail.getStowageNo() < 0){
					res.setMsg("库存不足!");
					res.setSuccess(false);
					return res;
				}
				if(already - deliveryDetail.getStowageNo() < 0){
					res.setMsg("库存不足!");
					res.setSuccess(false);
					return res;
				}
				iProductionDetailRepository.updateAlreadyOut(deliveryDetail.getStowageNo(), deliveryDetail.getOrderDetailUuid());
				iOrderDetailRepository.updateAlreadyOutByOrderDetailUuid(deliveryDetail.getOrderDetailUuid(),deliveryDetail.getStowageNo());
				iGoodsRepository.updateStowageNo(deliveryDetail.getStowageNo(), deliveryDetail.getGoodsUuid());
				item = item + deliveryDetail.getStowageNo();
				
			}
			
			iProduceRepository.updateAlreadyOut(item , orderNo);
			iTranslateRepository.changeExamineStatus(id);
			iOrderRepository.changeDeliveriesStatus(orderNo);
			res.setSuccess(true);
		} catch (Exception e) {
			res.setMsg(e.getMessage());
			res.setSuccess(false);
		}
		return res;
		
	}
	
	@Override
	@Transactional
	public ResponseMessage doSign(String deliveryNoteNo, String recipient,String signTime) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = sf.parse(signTime);
			Timestamp timestamp = new Timestamp(date.getTime());
			iTranslateRepository.doSign(deliveryNoteNo, recipient, timestamp);
			res.setSuccess(true);
		} catch (Exception e) {
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		return res;
	}
}
